package com.example.q_kang.hhh.setting;

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.view.OrderChooseDailog;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.base.App;
import com.sskj.base.bean.MasterUpBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.ArrayList;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/04/18
 */
public class SingleFragment extends MVPFragment<SinglePresenter> {


    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_choose_level)
    TextView tvChooseLevel;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private BottomSheetDialog bottomSheetDialog;
    private RelativeLayout rl_close;
    private RecyclerView recyclerView;
    private SlimAdapter<String> slimAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_single;
    }

    @Override
    public SinglePresenter getPresenter() {
        return new SinglePresenter();
    }

    @Override
    public void initView() {
        showBottomDialog();
        ClickUtil.click(300, tvChooseLevel, view -> {
            if (bottomSheetDialog != null) {
                bottomSheetDialog.show();
            }
        });
        ClickUtil.click(300, btnSubmit, view -> {
            if (TextUtils.isEmpty(getText(tvChooseLevel))
                    || getText(tvChooseLevel).equals(App.INSTANCE.getString(R.string.choose_level))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.choose_level));
                return;
            }
            mPresenter.masterChage(getText(tvChooseLevel));
        });
    }


    @Override
    public void initData() {
        mPresenter.getOrdersMoneys();
    }


    public static SingleFragment newInstance() {
        SingleFragment fragment = new SingleFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void updataMaster(MasterUpBean masterUpBean) {
        if (masterUpBean == null) {
            return;
        }
        if (slimAdapter != null) {
            slimAdapter.updateData(masterUpBean.getPrice());
        }
        tvAmount.setText(masterUpBean.getNow_price() + "$");
    }

    private void showBottomDialog() {
        bottomSheetDialog = new BottomSheetDialog(getActivity());
        View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.select_order_type, null);
        rl_close = dialogView.findViewById(R.id.rl_close);
        recyclerView = dialogView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 1))
                .setDividerColor(ContextCompat.getColor(getActivity(), R.color.divider_s))
                .setFirstDraw(false)
                .setLastDraw(false));
        slimAdapter = SlimAdapter.create().register(R.layout.item_order_choose, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_number, data)
                        .clicked(R.id.tv_number, view -> {
                            bottomSheetDialog.dismiss();
                            tvChooseLevel.setText(data);
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ClickUtil.click(300, rl_close, view -> bottomSheetDialog.dismiss());
        bottomSheetDialog.setContentView(dialogView);

    }

}
